package com.xjm.ips.service;

import com.xjm.ips.entity.AdjustSalary;
import com.xjm.ips.entity.MyPage;
import com.xjm.ips.mapper.AdjustSalaryMapper;
import com.xjm.ips.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class AdiustSalaryService {
    @Autowired
    AdjustSalaryMapper adjustSalaryMapper;
    @Autowired
    EmployeeMapper employeeMapper;
    public MyPage getEmployeeWithSalaryInner(Integer pageNo, Integer pageSize, AdjustSalary adjustSalary) {
        if(pageNo!=null && pageSize!=null){
            pageNo = (pageNo-1)*pageSize;
        }
        List<AdjustSalary> data = adjustSalaryMapper.selectEmployeeWithSalary(pageNo,pageSize,adjustSalary);
        Long total = adjustSalaryMapper.getTotalOfInner(adjustSalary);
        MyPage myPage = new MyPage();
        myPage.setData(data);
        myPage.setTotal(total);
        return myPage;
    }


    public Integer updateAdjustSalary(AdjustSalary adjustSalary) {
        return adjustSalaryMapper.updateByPrimaryKeySelective(adjustSalary);
    }


    public Integer addAdjustSalary(AdjustSalary adjustSalary) {
        adjustSalary.setAsDate(new Date());
        System.out.println(adjustSalary.getAsDate());
        return adjustSalaryMapper.insertSelective(adjustSalary);
    }

    public Integer deleteAdjustSalary(Integer id) {
        return adjustSalaryMapper.deleteByPrimaryKey(id);
    }

    @Transactional
    public AdjustSalary getNewestSalary(Integer eid){
        AdjustSalary adjustSalary = adjustSalaryMapper.selectNewestSalary(eid);
        if(adjustSalary==null){
            adjustSalary = adjustSalaryMapper.selectAllSalaryByEmpId(eid);
            adjustSalary.setEid(eid);
        }
        return adjustSalary;
    }
}
